angular.module("app.sys").register.controller("ListSysProjectCtrl", ["$scope", "$http", "$state", "$modal", "$modalService", function($scope, $http, $state, $modal, $modalService){
  $scope.stores = [];
  $scope.pager = {
    sizeOpts: [5, 10, 15, 20, 25, 30],
    size: 15,
    maxSize: 5,
    records: 0,
    page: 1,
    onChangeSize: function(){
      $scope.pager.page = 1;
      $scope.gets(1);
    },
    onChange: function(page){
      $scope.gets(page);
    },
    total: function(){
      return Math.ceil($scope.pager.records / $scope.pager.size);
    }
  };
  $scope.getCount = function(){
    $http.post('/sys/project/count/').success(function(ret){
      if(ret.status){
        $scope.pager.records = ret.data;
      }else{
        $modalService.myAlert("获取总数失败，系统错误！");
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.gets = function(page){
    $scope.loadTip = '正在获取数据...........';
    $scope.stores = [];
    $http.post('/sys/project/gets/', {
      page: page,
      size: $scope.pager.size
    }, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $scope.stores = ret.data;
        $scope.stores.length == 0 ? $scope.loadTip = '没有找到相关记录！！' : '';
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.refresh = function(){
    $scope.stores = [];
    $scope.gets(1);
    $scope.getCount();
  };
  //@formatter:off
  $scope.data = {
    entity:{info_type:'',status:''},
    opts:{
      infoOpts:[{k:'',t:'请选择'},{k:1,t:'短信营销(sms)'},{k:2,t:'彩信营销(mms)'},{k:3,t:'短信通知(sms)'}],
      statusOpts:[{k:'',t:'请选择'},{k:1,t:'成功'},{k:2,t:'正在执行'},{k:0,t:'未执行'},{k:-1,t:'删除'},{k:-2,t:'等待数据'},{k:-3,t:'数据上传完成'},{k:-4,t:'等待上传的数据处理完成'},{k:-5,t:'数据处理失败'}],
      statusOptd:[{k:'-1',t:'被删除'},{k:0,t:'未分析'},{k:1,t:'正在分析'},{k:2,t:'分析完成'}],
    }
  };
  //@formatter:on
  $scope.add = function(){
    var $modalInstance = $modal.open({
      templateUrl: "/sys/project/add/",
      controller: "AddSysProjectCtrl",
      backdrop: true,
      resolve: {
        data: function(){
          return $scope.data;
        }
      }
    });
    $modalInstance.result.then(function(status){
      if(status == 'success'){
        $scope.refresh();
      }
    }, function(status){
      if(status == 'success'){
        $scope.refresh();
      }
    });
  };
  $scope.edit = function(id){
    $http.post('/sys/project/get/', {
      id: id
    }, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $scope.entity = ret.data;
        var $modalInstance = $modal.open({
          templateUrl: "/sys/project/edit/",
          controller: "EditSysProjectCtrl",
          backdrop: true,
          resolve: {
            data: function(){
              return {
                entity: $scope.entity,
                opts: $scope.data.opts
              };
            }
          }
        });
        $modalInstance.result.then(function(status){
          if(status == 'success'){
            $scope.refresh();
          }
        }, function(status){
          if(status == 'success'){
            $scope.refresh();
          }
        });
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.log = function(projectId){
    $http.post('/sys/project/log/get/', {
      project_id: projectId
    }, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      var $modalInstance = $modal.open({
        templateUrl: "sys/project/log/",
        controller: "SysProjectTempLogMangerCtrl",
        backdrop: true,
        resolve: {
          data: function(){
            return {
              entity: ret.data,
              opts: $scope.data.opts
            };
          }
        }
      });
      $modalInstance.result.then(function(status){
        if(status == 'success'){
          $scope.refresh();
        }
      }, function(status){
        if(status == 'success'){
          $scope.refresh();
        }
      });
      // $modalService.myAlert(ret.data);
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
  $scope.remove = function(id){
    $modalService.myConfirm("删除以后内容不能恢复，您是否确认删除？", function(){
      $http.post('/sys/project/remove/', {
        id: id
      }, {
        transformRequest: function(data){
          return $.param(data);
        }
      }).success(function(ret){
        if(ret.status){
          $modalService.myAlert('删除成功!');
          $scope.refresh();
        }else{
          $modalService.myAlert(ret.data);
        }
      }).error(function(){
        $modalService.myAlert('系统错误，请求资源失败！');
      });
    }, function(){
    });
  };
  $scope.getCount();
  $scope.gets(1);
}]);
angular.module("app.sys").register.controller("AddSysProjectCtrl", ["$scope", "$http", "$modalInstance", "$modalService", "data", function($scope, $http, $modalInstance, $modalService, data){
  $scope.entity = data.entity;
  $scope.opts = data.opts;
  $scope.cancel = function(){
    $modalInstance.dismiss("cancel");
  };
  $scope.submit = function(){
    $http.post('/sys/project/add', $scope.entity, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      if(ret.status){
        $modalService.myAlert(ret.data);
        $modalInstance.close('success');
      }else{
        $modalService.myAlert(ret.data);
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
}]);
angular.module("app.sys").register.controller("EditSysProjectCtrl", ["$scope", "$http", "$modalInstance", "$modalService", "data", function($scope, $http, $modalInstance, $modalService, data){
  $scope.entity = data.entity;
  $scope.opts = data.opts;
  $scope.cancel = function(){
    $modalInstance.dismiss("cancel");
  };
  $scope.submit = function(){
    $http.post('/sys/project/edit/', $scope.entity, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      $modalService.myAlert(ret.data);
      if(ret.status){
        $modalInstance.close('success');
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
}]);
angular.module("app.sys").register.controller("SysProjectTempLogMangerCtrl", ["$scope", "$http", "$modalInstance", "$modalService", "data", function($scope, $http, $modalInstance, $modalService, data){
  $scope.entity = data.entity;
  $scope.opts = data.opts;
  $scope.cancel = function(){
    $modalInstance.dismiss("cancel");
  };
  $scope.submit = function(){
    $http.post('/sys/project/log/submit/', $scope.entity, {
      transformRequest: function(data){
        return $.param(data);
      }
    }).success(function(ret){
      $modalService.myAlert(ret.data);
      if(ret.status){
        $modalInstance.close('success');
      }
    }).error(function(){
      $modalService.myAlert('系统错误，请求资源失败！');
    });
  };
}]);
